package com.yunxi.poc.starbucks.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;

@Configuration
public class ExecutorConfig {
    private static final Logger logger = LoggerFactory.getLogger(ExecutorConfig.class);

    @Value("${async.executor.thread.core_pool_size}")
    private int corePoolSize;
    @Value("${async.executor.thread.name.prefix}")
    private String namePrefix;

    @Bean(name = "executorService")
    public Executor executorService() {
        logger.info("start executorService...");
        ThreadPoolTaskExecutor executorService = new ThreadPoolTaskExecutor();
        // 配置核心线程数
        executorService.setCorePoolSize(corePoolSize);
        // 配置线程池中的线程的名称前缀
        executorService.setThreadNamePrefix(namePrefix);
        // 执行初始化
        executorService.initialize();
        return executorService;
    }
}
